<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="货柜名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="containerName">
              <a-input v-model="model.containerName" placeholder="请输入货柜名称"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="商品类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="containerType">
              <a-input v-model="model.containerType" placeholder="请输入商品类型"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item
              v-if="model.id"
              label="货柜编号"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="containerNo"
            >
              <a-input v-model="model.containerNo" placeholder="请输入货柜编号"></a-input>
            </a-form-model-item>

            <a-form-model-item v-else label="货柜编号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="containerNo">
              <a-select v-model:value="model.containerNo" placeholder="请选择货柜编号">
                <a-select-option v-for="item in containerList" :key="item.id" :value="item.containerNo"
                  >{{ item.containerNo }}-{{ item.containerName }}- 货位数量{{ item.postionSums }}</a-select-option
                >
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="条码标识" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="containerBarCode">
              <a-input v-model="model.containerBarCode" placeholder="请输入条码标识"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="货柜版本" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="containerVersion">
              <a-input v-model="model.containerVersion" placeholder="请输入货柜版本"></a-input>
            </a-form-model-item>
          </a-col>
          <!-- <a-col :span="24">
            <a-form-model-item label="货柜重量" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="containerWeight">
              <a-input-number v-model="model.containerWeight" placeholder="请输入货柜重量" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="货柜长度" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="containerLength">
              <a-input-number v-model="model.containerLength" placeholder="请输入货柜长度" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="货柜宽度" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="containerWidth">
              <a-input-number v-model="model.containerWidth" placeholder="请输入货柜宽度" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="货柜高度" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="containerHeight">
              <a-input-number v-model="model.containerHeight" placeholder="请输入货柜高度" style="width: 100%" />
            </a-form-model-item>
          </a-col> -->
          <a-col :span="24">
            <a-form-model-item label="货位数量" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="postionSums">
              <a-input-number v-model="model.postionSums" placeholder="请输入货位数量" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <!-- <a-col :span="24">
            <a-form-model-item label="生产日期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="manufacture">
              <a-input v-model="model.manufacture" placeholder="请输入生产日期"  ></a-input>
            </a-form-model-item>
          </a-col> -->
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>
import { httpAction, getAction } from '@/api/manage'
import { validateDuplicateValue } from '@/utils/util'

export default {
  name: 'BmsContainerInfoForm',
  components: {},
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false,
    },
  },
  data() {
    return {
      model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      confirmLoading: false,
      validatorRules: {
        containerName: [{ required: true, message: '请填写货柜名称!' }],
        // containerCode: [{ required: true, message: '请填写货位序号!' }],
        containerNo: [{ required: true, message: '请选择货位序号!' }],
      },
      url: {
        add: '/bms/bmsContainerInfo/add',
        edit: '/bms/bmsContainerInfo/edit',
        queryById: '/bms/bmsContainerInfo/queryById',
      },
      containerList: [],
    }
  },
  computed: {
    formDisabled() {
      return this.disabled
    },
  },
  created() {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model))
  },
  methods: {
    add(list) {
      this.containerList = list
      this.edit(this.modelDefault)
    },
    edit(record) {
      this.model = Object.assign({}, record)
      this.visible = true
    },
    submitForm() {
      const that = this
      // 触发表单验证
      this.$refs.form.validate((valid) => {
        if (valid) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          httpAction(httpurl, this.model, method)
            .then((res) => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
              } else {
                that.$message.warning(res.message)
              }
            })
            .finally(() => {
              that.confirmLoading = false
            })
        }
      })
    },
  },
}
</script>
